package com.business.service.impl;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.business.dao.TagItemMapper;
import com.business.domain.TagItem;
import com.business.domain.TagItemExample;
import com.business.domain.TagItemExample.Criteria;
import com.business.service.TagItemService;
import com.sys.result.PageParam;
import com.sys.result.RequestPageVo;
import com.sys.result.RequestResultVo;

@Service
public class TagItemServiceImpl implements TagItemService{

	@Resource
	private TagItemMapper tagItemMapper;
	
	@Override
	public RequestPageVo<TagItem> queryTagItemList(PageParam param,Integer tagId,String name) {
		RequestPageVo<TagItem> pageVo=new RequestPageVo<TagItem>();
		
		TagItemExample example=new TagItemExample();
		Criteria criteria=example.createCriteria();
		
		if(!StringUtils.isEmpty(name)){
			criteria.andNameLike("%"+name+"%");
		}
		
		if(tagId!=null){
			criteria.andTagIdEqualTo(tagId);
		}
		
		int count=tagItemMapper.countByExample(example);
		if(count>0){
			example.setPageSize(param.getLimit());
			example.setStartPage(param.getOffset());
			List<TagItem> TagList=tagItemMapper.selectByExample(example);
			pageVo.setLimit(param.getLimit());
			pageVo.setTotal(count);
			pageVo.setOffset(param.getOffset());
			
			pageVo.setRows(TagList);
		}
		
		return pageVo;
	}

	@Override
	public List<TagItem> queryTagItemByTagCode(String TagCode) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public RequestResultVo<TagItem> addTagItem(TagItem tagItem) {
		RequestResultVo<TagItem> result=new RequestResultVo<TagItem>();
		
		TagItemExample example =new TagItemExample();
		Criteria criteria=example.createCriteria();
		criteria.andNameEqualTo(tagItem.getName());
		List<TagItem> list=tagItemMapper.selectByExample(example);
		try {
				
			if(list!=null&&list.size()>0){//判断编码是否重复
				result.setSuccess(false);
				result.setErrorMessage("该标签值已存在，请勿重复添加！");
				return result;
			}else{
				// 添加
				tagItem.setCreatime(new Date());
				tagItemMapper.insertSelective(tagItem);
				result.setSuccess(true);
			}
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorMessage("添加失败!");
		} 
		return result;
		
	}
		

	@Override
	public RequestResultVo<TagItem> updateTagItem(TagItem tagItem) {
		RequestResultVo<TagItem> result=new RequestResultVo<TagItem>();
		
		TagItemExample example =new TagItemExample();
		Criteria criteria=example.createCriteria();
		criteria.andNameEqualTo(tagItem.getName());
		List<TagItem> list=tagItemMapper.selectByExample(example);
		try{
			for(Iterator<TagItem> itor=list.iterator();itor.hasNext();){
				TagItem t=itor.next();
				if(t.getId()!=tagItem.getId()){//标签值修改了
					if(list.size()>0){
						result.setSuccess(false);
						result.setErrorMessage("该标签值已存在，请勿重复！");
						return result;
					}
				}
			}
			tagItemMapper.updateByPrimaryKeySelective(tagItem);
			result.setSuccess(true);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorMessage("修改失败!");
		} 
		return result;
	
	}

	@Override
	public RequestResultVo<TagItem> deleteTagItem(Integer id) {
		RequestResultVo<TagItem> result=new RequestResultVo<TagItem>();
		try {
			tagItemMapper.deleteByPrimaryKey(id);
			result.setSuccess(true);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorMessage("删除失败!");
		}
		return result;
	}

	
}
